unit uSettingsForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Spin, ComCtrls,
  TntForms, TntStdCtrls, TntClasses,
  uInterfaces, TntComCtrls, TntExtCtrls, Buttons, TntButtons;

type
  TFormSettings = class(TTntForm)
    PageControl: TTntPageControl;
    TabGeneral: TTntTabSheet;
    TabAdditional: TTntTabSheet;
    CheckBox4: TTntCheckBox;
    PanelBottom: TTntPanel;
    Label6: TTntLabel;
    MemoGreeting: TTntMemo;
    Label7: TTntLabel;
    MemoCongratulation: TTntMemo;
    Label5: TTntLabel;
    MemoDescription: TTntEdit;
    CheckBox2: TTntCheckBox;
    CheckBox1: TTntCheckBox;
    PanelTime: TTntPanel;
    CheckBox3: TTntCheckBox;
    ButtonOK: TTntButton;
    ButtonCancel: TTntButton;
    SpinHours: TSpinEdit;
    SpinMinutes: TSpinEdit;
    TntLabel1: TTntLabel;
    TntLabel2: TTntLabel;
    TntLabel3: TTntLabel;
    procedure ButtonOKClick(Sender: TObject);
  private
    fTest: IgtTest;
  private
    procedure ReadTest;
    procedure WriteTest;
    function GetTestTime: Integer;
    procedure SetTestTime(const Value: Integer);
    property TestTime: Integer read GetTestTime write SetTestTime;
  public
    constructor Create(AOwner: TComponent; Test: IgtTest);
    destructor Destroy; override;
  end;

implementation

uses
  uUtils,
  uTexts;

{$R *.dfm}

{ TFormSettings }

constructor TFormSettings.Create(AOwner: TComponent; Test: IgtTest);
begin
  inherited Create(AOwner);
  fTest := Test;
  ReadTest;
end;

destructor TFormSettings.Destroy;
begin
  fTest := nil;
  inherited;
end;

function TFormSettings.GetTestTime: Integer;
var
  hours, minutes: Integer;
begin
  try
    hours := SpinHours.Value * 3600;
  except
    hours := 0;
  end;
  try
    minutes := SpinMinutes.Value * 60;
  except
    minutes := 0;
  end;
  Result := hours + minutes;
end;


procedure TFormSettings.ReadTest;
begin
  MemoDescription.Text := fTest.Name;
  MemoGreeting.Text := fTest.Greeting;
  MemoCongratulation.Text := fTest.Congratulations;
  TestTime := fTest.TimeLimit;
  CheckBox1.Checked := fTest.MixQuestions;
  CheckBox2.Checked := fTest.MixVariants;
  CheckBox3.Checked := fTest.CanClose;
  CheckBox4.Checked := fTest.ShowComment;
end;

procedure TFormSettings.SetTestTime(const Value: Integer);
begin
  SpinHours.Value := Value div 3600;
  SpinMinutes.Value := (Value - SpinHours.Value * 3600) div 60;
end;

procedure TFormSettings.WriteTest;
begin
  fTest.Name := Trim(MemoDescription.Text);
  fTest.Greeting := MemoGreeting.Text;
  fTest.Congratulations := MemoCongratulation.Text;
  fTest.TimeLimit := TestTime;
  fTest.MixQuestions := CheckBox1.Checked;
  fTest.MixVariants := CheckBox2.Checked;
  fTest.CanClose := CheckBox1.Checked;
  fTest.ShowComment := CheckBox4.Checked;
  fTest.MaxBreaks := 10000;
  fTest.MaxPassings := 1;
end;

procedure TFormSettings.ButtonOKClick(Sender: TObject);
begin
  if Trim(MemoDescription.Text) = '' then
    begin
    ShowWarningMessage(cEmptyTestName);
    Exit;
    end;
  if not IsShortText(Trim(MemoDescription.Text)) then
    begin
    ShowWarningMessage(cLongTestName);
    Exit;
    end;
  WriteTest;
  ModalResult := mrOK;
end;

end.
